/*
 * Alloy Analyzer
 * Copyright (c) 2002 Massachusetts Institute of Technology
 *
 * The Alloy Analyzer is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * The Alloy Analyzer is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with the Alloy Analyzer; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package alloy.util;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;

/** If an Internet connection is available, checks if
    an updated version of Alloy Analyzer has been
    posted on the distribution website.  If so,
    tells the user to download the update.  */
public class SelfUpdater {
    public static void checkForUpdate() {
	try {
	    URL yahoo = new URL("http://sdg.lcs.mit.edu/newalloy/Version.java");
	    BufferedReader in =
		new BufferedReader(new InputStreamReader(yahoo.openStream()));
	    
	    String inputLine;
	    
	    while ((inputLine = in.readLine()) != null) {
		int buildDateStart = inputLine.indexOf("\"");
		if (buildDateStart >= 0) {
		    int buildDateEnd = inputLine.lastIndexOf("\"");
		    String buildDate = inputLine.substring(buildDateStart+1, buildDateEnd);
		    if (!buildDate.equals(Version.BUILD_DATE)) {
			System.out.println();
			System.out.println("*********************************************");
			System.out.println("* An updated version of Alloy is available. *");
			System.out.println("* Please get it from                        *");
			System.out.println("* http://sdg.lcs.mit.edu/newalloy           *");
			System.out.println("*********************************************");
			System.out.println();
		    }
		}
	    }
	    
	    in.close();
	} catch (Exception e_) { System.out.println("Unabled to check for update."); }
    }
}
